package com.carlinfo.userscenter.users.dao.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.carlinfo.userscenter.commom.util.DbUtil;
import com.carlinfo.userscenter.users.dao.IARoleDao;
import com.carlinfo.userscenter.users.pojo.ARole;


public class ARoleDaoImpl implements IARoleDao {
	private DbUtil dbutil = DbUtil.getInstance();

	/**
	 * ��ѯһ����¼���������ݿ��еĶ���ת����java����
	 * 
	 * @param role
	 * @param resultset
	 * @throws SQLException
	 */
	private ARole convertFromDb_resource(ResultSet resultset) throws SQLException {
		/* �Ѳ�ѯ���Ľ�����java��¼ */
		ARole role = new ARole();
		int id = resultset.getInt("id");
		String name = resultset.getString("name");
		String content = resultset.getString("content");
		byte status = resultset.getByte("status");
		Date createTime = resultset.getTimestamp("createTime");
		Date updateTime = resultset.getTimestamp("updateTime");
		Date pubTime = resultset.getTimestamp("pubTime");

		role.setId(id);
		role.setContent(content);
		role.setCreateTime(createTime);
		role.setName(name);
		role.setPubTime(pubTime);
		role.setStatus(status);
		role.setUpdateTime(updateTime);
		return role;
	}

	/**
	 * ͨ�������������������
	 * 
	 * @param resultset
	 * @return
	 * @throws SQLException
	 */
	private ARole convertFromDb(ResultSet resultset) throws SQLException {
		Class roleClass = ARole.class;
		Object roleObj = null;
		Object dbObject = null;
		try {
			/* ʵ����һ������ */
			roleObj = roleClass.newInstance();
			Field[] fields = roleClass.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				String name = field.getName();
				Method method = null;
				/* ��װ������������set�����ҵ�һ����ĸҪ��д */
				String setName = "set" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
				/* ��ȡ���Ե�����field.getType() */
				System.out.println("==" + name + "==" + setName + "==" + field.getType().getSimpleName());
				Class fieldType = field.getType();
				/* ������������ȡ��Ӧ�ķ��� */
				if (fieldType == int.class || fieldType == Integer.class) {
					method = roleClass.getMethod(setName, int.class);
					dbObject = resultset.getInt(name);
				} else if (fieldType == byte.class || fieldType == Byte.class) {
					method = roleClass.getMethod(setName, byte.class);
					dbObject = resultset.getByte(name);
				} else if (fieldType == String.class) {
					method = roleClass.getMethod(setName, String.class);
					dbObject = resultset.getString(name);
				} else if (fieldType == Date.class) {
					method = roleClass.getMethod(setName, Date.class);
					dbObject = resultset.getTimestamp(name);
				}
				method.invoke(roleObj, dbObject);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return (ARole) roleObj;
	}

	@Override
	public int saveOne(ARole role) {
		int res = 0;
		// TODO Auto-generated method stub
		System.out.println("==�ұ����˰�===");
		String sql = "INSERT INTO a_role(name,content,status,createTime,updateTime,pubTime)" + "VALUES (?,?,?,?,?,?)";
		Connection conn = this.dbutil.getConnection();
		ResultSet resultset = null;
		PreparedStatement pr = null;
		try {
			/* �ڶ���������ʲô��˼�� */
			pr = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			pr.setObject(1, role.getName());
			pr.setObject(2, role.getContent());
			pr.setObject(3, role.getStatus());
			pr.setObject(4, role.getCreateTime());
			pr.setObject(5, role.getUpdateTime());
			pr.setObject(6, role.getPubTime());
			res = pr.executeUpdate();
			/* ��ȡ���� */
			/**
			 * ��ѯ����������ͨ���������ַ�ʽ SELECT LAST_INSERT_ID(); SELECT @@identity;
			 */
			resultset = pr.getGeneratedKeys();
			if (resultset.next()) {
				int id = resultset.getInt(1);
				role.setId(id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.dbutil.closeAll(conn, pr, resultset);
		}
		return res;
	}

	@Override
	public int updateOne(ARole role) {
		// TODO Auto-generated method stub
		System.out.println("�Ҹ�����");
		return 0;
	}

	@Override
	public int deleteOne(ARole role) {
		// TODO Auto-generated method stub
		System.out.println("��ɾ����");
		return 0;
	}

	@Override
	public ARole findOne(Map<String, Object> map) {
		ARole role = new ARole();
		// TODO Auto-generated method stub
		System.out.println("���ҵ���");
		List<Object> paramsLIst = new ArrayList<Object>();
		String sql = "select * from a_role ";
		
		if(map.get("id")!=null && !"".equalsIgnoreCase(map.get("id")+"")) {
			sql+=  "where id = ?";
			paramsLIst.add(map.get("id"));
		}else if(map.get("name")!=null && !"".equalsIgnoreCase(map.get("name")+"")){
			sql+=  "where name = ?";
			paramsLIst.add(map.get("name"));
		}
		Connection conn = this.dbutil.getConnection();
		ResultSet resultset = null;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			/* ��id���в�ѯ */
//			ps.setObject(1, map.get("id"));
			/* ����Ϊ���Ը�ֵ */
			int count = 1;
			for (Object object : paramsLIst) {
				ps.setObject(count, object);
				count++;
			}
			resultset = ps.executeQuery();
			if (resultset.next()) {
				role = convertFromDb(resultset);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.dbutil.closeAll(conn, ps, resultset);
		}

		return role;
	}

	@Override
	public List<ARole> findCondList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		System.out.println("���ҵ��˺ܶ�");
		ARole role = new ARole();
		List<ARole> roleList = new ArrayList<ARole>();
		// TODO Auto-generated method stub

		String sql = "select * from a_role ";
		
		Connection conn = this.dbutil.getConnection();
		ResultSet resultset = null;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			/* ��id���в�ѯ */
//			ps.setObject(1, map.get("id"));
			/* ����Ϊ���Ը�ֵ */

			resultset = ps.executeQuery();
			while (resultset.next()) {
				role = convertFromDb(resultset);
				/* ��role���ڼ����� */
				roleList.add(role);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			this.dbutil.closeAll(conn, ps, resultset);
		}
		return roleList;
	}

}
